// Copyright (c) Microsoft Corporation.
// 
// Author: Chris King
// Date: 5/2007

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextManager.Interop;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

/// <summary>
/// Used by the Hyperlink and Browser classes 
/// </summary>
internal static class Services
{
    #region Static Public Data Members
    public static readonly Guid CSharpLibrary = new Guid("58f1bad0-2288-45b9-ac3a-d56398f7781d");
    public static readonly Guid VBLibrary = new Guid("414AC972-9829-4B6A-A8D7-A08152FEB8AA");
    public static readonly Guid CPPLibrary = new Guid("6C1AC90E-09FC-4F23-90FF-87F8CFC2A445");
    public static readonly string CSharpLanguage = "CSharp";
    public static readonly string CPPLanguage = "C/C++";
    public static readonly string VBLanguage = "Basic";
    #endregion

    #region Private Static Members
    private static T GetManager<T, V>(DTE2 dte)
    {
        IOleServiceProvider sp = (IOleServiceProvider)dte;
        Guid iid = typeof(T).GUID;
        Guid service = typeof(V).GUID;
        IntPtr pUnk;
        sp.QueryService(ref service, ref iid, out pUnk);
        T manager = (T)Marshal.GetObjectForIUnknown(pUnk);
        return manager;
    }
    #endregion

    #region Public Static Members
    public static IVsObjectManager2 GetIVsObjectManager2(DTE2 dte)
    {
        return GetManager<IVsObjectManager2, SVsObjectManager>(dte);
    }
    public static IVsTextManager GetIVsTextManager(DTE2 dte)
    {
        return GetManager<IVsTextManager, SVsTextManager>(dte);
    }
    public static IVsRunningDocumentTable GetRunningDocumentTable(DTE2 dte)
    {
        return GetManager<IVsRunningDocumentTable, SVsRunningDocumentTable>(dte);
    }
    public static IVsLibrary2 GetIVsLibrary2(DTE2 dte, Guid guid)
    {
        IVsObjectManager2 objectManager = GetIVsObjectManager2(dte);
        IVsLibrary2 library = null;
        objectManager.FindLibrary(ref guid, out library);
        return library;
    }
    public static List<KeyValuePair<IVsLibrary2, string>> GetIVsLibraries(DTE2 dte)
    {
        List<KeyValuePair<IVsLibrary2, string>> list = new List<KeyValuePair<IVsLibrary2, string>>();
        list.Add(new KeyValuePair<IVsLibrary2, string>(GetIVsLibrary2(dte, CSharpLibrary), "."));
        list.Add(new KeyValuePair<IVsLibrary2, string>(GetIVsLibrary2(dte, CPPLibrary), "::"));
        list.Add(new KeyValuePair<IVsLibrary2, string>(GetIVsLibrary2(dte, VBLibrary), "."));
        return list;
    }
    #endregion
}

